package com.zzn.bookManage.util;

import java.io.IOException;
import java.util.Properties;

public class MyProperties {

    private String appName;
    private String dbFile;
    private String dbRestore;
    private String borrowDuration;

    private static MyProperties instance;

    /**
     * 单例模式 优化对象创建
     * @return
     */
    public static MyProperties getInstance(String configFile){
        if (instance == null){
            instance = new MyProperties(configFile);
        }
        return instance;
    }

    private MyProperties(String filename){
        try {
            if (StringUtil.isBlank(filename)){
                filename = "config.properties";
            }
            Properties properties = new Properties();
            properties.load(MyProperties.class.getClassLoader().getResourceAsStream(filename));
            this.appName = properties.getProperty("appName","appName");
            this.dbFile = properties.getProperty("dbFile","appData.db");
            this.dbRestore = properties.getProperty("dbRestore","false");
            this.borrowDuration = properties.getProperty("borrowDuration","14");

            System.out.println("==== load system config success ====");
            System.out.println("== 应用名称：\t"+this.appName);
            System.out.println("== 数据库文件：\t"+this.dbFile);
            System.out.println("== 清空数据库：\t"+this.dbRestore);

        } catch (IOException e) {
            System.out.println("!!!! 找不到配置文件 !!!!");
            System.exit(1);
        }
    }

    public String getAppName() {
        return appName;
    }

    public String getDbFile() {
        return dbFile;
    }

    public String getDbRestore() {
        return dbRestore;
    }

    public String getBorrowDuration() {
        return borrowDuration;
    }
}
